#!/usr/bin/env bash

set -e

app=$1
port=${2:-8080}

root="$(git rev-parse --show-toplevel)"
. "$root/scripts/cd-and-back.sh"
cd "$root/e2e"

test () {
	NODE_NO_WARNINGS=1 \
	bun run start-test "CY_TEST=true bundle exec $root/gem/bin/pagy $app -p $port -q > /dev/null" "http://0.0.0.0:$port" \
	                   "cypress run --quiet --config baseUrl=http://127.0.0.1:$port --spec cypress/e2e/$app.cy.ts"
}

if [[ -z $app ]]
then
	for app in demo repro rails calendar keynav
	do
		port=$((port+1))
		test &
	done
	for p in $(jobs -p)
	do
		wait "$p" || { echo ">>> Test job $p failed! <<<" >&2; fail=1; }
	done
	[[ -z $fail ]] || exit 1
else
	test
fi
